package ar.edu.taxi.persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class DAOPositions extends DataAccesObject implements IfDAO{

	@Override
	public int delete(String condicion) {
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "DELETE FROM positions " +
						"WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

	@Override
	public IfDTO find(String codigo) {
		DTOPositions position = null;
		Connection con = getConexion();
		if(codigo == null || codigo.length() == 0) return null;
		String query = "SELECT * FROM positions " +
						"WHERE " + codigo;
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				position = new DTOPositions(rs.getInt("id"), rs.getInt("idVehicle"), 
						rs.getInt("idDriver"), rs.getBoolean("status"), rs.getTime("date"), 
						rs.getDouble("latitude"), rs.getDouble("longitude"));
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return position;
	}

	@Override
	public int insert(IfDTO bean) {
		int numFilas = 0;
		DTOPositions position = (DTOPositions) bean;
		Connection con = getConexion();
		String query = "INSERT INTO positions VALUES( DEFAULT"+
						", '"+position.getIdVehicle() + "'" +
						", '"+position.getIdDriver() + "'" +
						", '"+(position.isStatus()? 1:0 ) + "'" +
						", '"+position.getDateToString() + "'" +
						", '"+position.getLatitude() + "'" +
						", '"+position.getLongitude() + "'" +
						" )";
		try {
			Statement sentencia = con.createStatement();
			numFilas = sentencia.executeUpdate(query);
			sentencia.close();
			cerrarConexion( con );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return numFilas;
	}

	@Override
	public Vector<IfDTO> select(String condicion) {
		Vector<IfDTO> positions = new Vector<IfDTO>();
		Connection con = getConexion();
		String query = "SELECT * FROM positions " +
						(condicion == null || condicion.length() == 0 ? "" : "WHERE " + condicion) +
						" ORDER BY id";
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				DTOPositions position = new DTOPositions(rs.getInt("id"), rs.getInt("idVehicle"), 
						rs.getInt("idDriver"), rs.getBoolean("status"), rs.getTime("date"), 
						rs.getDouble("latitude"), rs.getDouble("longitude"));
				positions.add(position);
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return positions;
	}

	@Override
	public int update(IfDTO dto, String condicion) {
		DTOPositions position = (DTOPositions) dto;
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "UPDATE positions SET" +
						" idVehicle='" + position.getIdVehicle() + "'" +
						" , idDriver='" + position.getIdDriver() + "'" +
						" , status='" + (position.isStatus()? 1:0 ) + "'" +
						" , date='" + position.getDateToString() + "'" +
						" , latitude='" + position.getLatitude() + "'" +
						" , longitude='" + position.getLongitude() + "'" +
						" WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}
}
